<template>
	<view >
		<MyInput 
			label="名字" 
			:borderColor="primary_cool_color"
			v-model="name" 
			:placeholder="setBillType?.name"/>
	</view>
</template>

<script setup>
	import { ref , watch , defineEmits} from "vue"
	
	import MyInput from '@/components/MyInput/MyInput.vue'
	
	import {
		DormitoryModel
	} from '@/service/model/dormitory.js'
	
	import {
		verifyIsLogin
	} from '@/hooks/verify.js'
	
	import {
		dynamicStyleStore
	} from '@/stores/dynamicStyle.js'
	
	const dormitoryModel = new DormitoryModel()
	const dynamicStyle_store = dynamicStyleStore()
	
	const {
		primary_cool_color
	} = dynamicStyle_store.dormitoryStyle	
	
	const props=defineProps({
		clickSubmitBillTypeFlag:{
			type:Number
		},
		setBillType:{
			type:Object
		}
	})
	const emits=defineEmits(['getBillTypeListEmit'])
	
	const dormitoryAllId = uni.getStorageSync('homeData')
	
	watch(()=>props.clickSubmitBillTypeFlag,()=>{
		submitName()
		name.value=''
	})
	
	watch(()=>props.setBillType,()=>{
		name.value=''
	})
	
	const name = ref('')
	
	//提交修改名字
	const submitName=async()=>{
		if(!name.value.trim()) {
			uni.$emit('tips','请输入有效类型名','warn')
			return
		}
		if(props.setBillType?.id){
			const {data,success} = await dormitoryModel.patchBillType({
				name:name.value,
				in_out_type:props.setBillType.in_out_type
			},props.setBillType.id,dormitoryAllId.dormitory_id)
			if(success){
				uni.$emit('tips',data,'success')
				emits('getBillTypeListEmit',props.setBillType.id)
			}
		}else{
			const {data,success} = await dormitoryModel.postAddBillType({
				name:name.value,
				in_out_type:props.setBillType.in_out_type
			},dormitoryAllId.dormitory_id)
			if(success){
				uni.$emit('tips','创建成功','success')
				emits('getBillTypeListEmit',data)
			}
		}
	}
</script>

<style lang="less">
	
</style>